# -*- coding: utf-8 -*-
##############################################################################
#    
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
#
##############################################################################

#-------------------------------------------------------------
#Indonesian
#-------------------------------------------------------------
##### License from http://riri.blogdetik.com/2009/02/23/php-convertion-number-to-words-indonesian/ by Riri

def amount_to_text(num):
    indo_array = ["Satu", "Dua", "Tiga", "Empat", "Lima", "Enam","Tujuh", "Delapan", "Sembilan"]
    res = ""
    #1. Ratusan Juta (Kuadran I) - Hundred Millenium
    kw1 = int(num/1000000.0)
    num = num - kw1*1000000
    if (kw1>0):
        res = res + numtoword(kw1) + " Juta "
    
    #2. Jutaan (Kuadran II ) -  Millenium
    kw2 = int(num/1000.0)
    num = num - kw2 *1000
    if (kw2>0):
        res = res + numtoword(kw2) + " Ribu "
    
    ratus = int(num/100.0)
    num = num - ratus*100
    if (ratus):
        if (ratus-1)>=0 and (ratus-1)<len(indo_array):
            ratus = indo_array[ratus-1]
        else:
            ratus = ""
        res = res + ratus + " Ratus "

    #B. cari bahasa puluhan - Tens
    puluh = int(num/10.0)
    num = num - puluh*10
    if (puluh):
        if (puluh>1):
            puluh = indo_array[puluh-1]
            res = res + puluh + " Puluh "
        else:
            ada = 0
            if (num-1)>=0 and (num-1)<len(indo_array):
                satuan = indo_array [num-1]    
            else:
                satuan = ""
            res = res + satuan + "belas "
    #C. cari bahasa satuan - Unit
    sat = num
    if (puluh or sat):
        if (res):
            res = res + " "
        if puluh<2 or type(puluh)==type('satus'):
            try:
                if (puluh*10+sat-1)>=0 and (puluh*10+sat-1)<len(indo_array):
                    res = res + indo_array[puluh*10 + sat -1]                
            except:
                if (sat-1)>=0 and (sat-1)<len(indo_array):
                    res = res + indo_array[sat -1]
        else:
            try:
                if (puluh-1)>=0 or (puluh-1)<len(indo_array):
                    res = res + indo_array[puluh-1] + "belas"
            except:
                res = res + "belas"
            if (sat):
                if (sat-1)>=0 and (sat-1)<len(indo_array):
                    res = res + "-" + indo_array[sat-1]

    res = res.replace("belas","Sepuluh")
    res = res.replace("satu belas","Sebelas")
    res = res.replace("satu ratus", "Seratus")
    res = res.replace("satu ribu", "Seribu")
    res = res.replace("  ", " ").strip()
    return res



